﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSAMMatrixCloud.Common
{
    public class Check
    {
        #region ---Checksum---
        /// <summary>
        /// Check the command 
        /// </summary>
        /// <param name="buffer">check data buffer</param>
        /// <param name="start">check start</param>
        /// <param name="length">check length</param>
        /// <returns>return checksum</returns>
        public static int Checksum(byte[] buffer, int start, int length)
        {
            int inttemp = 0;
            for (int i = start; i < start + length; i++)
            {
                inttemp += buffer[i];
                if (inttemp >= 256) inttemp = inttemp - 256;
            }
            return inttemp == 0 ? 0 : inttemp;
        }
        #endregion

        #region ---CheckXor---
        /// <summary>
        /// Check the command 
        /// </summary>
        /// <param name="buffer">check data buffer</param>
        /// <param name="start">check start</param>
        /// <param name="length">check length</param>
        /// <returns>return checksum</returns> 
        public static int CheckXor(byte[] buffer, int start, int length)
        {
            if (buffer.Length == 0)
                return 0;
            byte inttemp = (byte)buffer[start];
            for (int i = start+1; i < start+length; i++)
            {
                inttemp ^= buffer[i];
            }
            return inttemp;
        }
        #endregion

        #region ---Public Funcation---
        /// <summary>
        /// 十六进制转换字节数组
        /// </summary>
        /// <param name="hexStr">十六进制字符串</param>
        /// <returns></returns>
        public static byte[] HexStringToByteArray(string hexStr)
        {
            hexStr = hexStr.Replace(" ", "");
            byte[] bytResult = new byte[hexStr.Length / 2];
            try
            {
                for (int i = 0; i < hexStr.Length; i += 2)
                {
                    bytResult[i / 2] = (byte)Convert.ToByte(hexStr.Substring(i, 2), 16);
                }
            }
            catch { }
            return bytResult;
        }
        /// <summary>
        /// 字节数组转换十六进制
        /// </summary>
        /// <param name="bytArray">字节数组</param>
        /// <returns></returns>
        public static string ByteArrayToHexString(byte[] bytArray)
        {
            StringBuilder sbResult = new StringBuilder(bytArray.Length * 2);
            try
            {
                foreach (byte b in bytArray)
                {
                    sbResult.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
                }
            }
            catch { }
            return sbResult.ToString().ToUpper();
        }
        /// <summary>
        /// 字节数组转换十六进制
        /// </summary>
        /// <param name="bytArray">字节数组</param>
        /// <returns></returns>
        public static string ByteArrayToHexString(byte[] bytArray, int start, int length)
        {
            StringBuilder sbResult = new StringBuilder(bytArray.Length * 2);
            try
            {
                for (int i = start; i < length + start; i++)
                {
                    sbResult.Append(Convert.ToString(bytArray[i], 16).PadLeft(2, '0'));
                }
            }
            catch { }
            return sbResult.ToString().ToUpper();
        }
        #endregion
    }
}
